package com.android.qjpmc.widget.manager.retrofit;

import com.android.qjpmc.widget.retrofithelper.http.ApiServer;
import com.android.qjpmc.widget.retrofithelper.http.Url;
import com.android.qjpmc.widget.utils.FileUtils;
import com.android.qjpmc.widget.retrofithelper.interceptor.CacheOfflineInterceptor;
import com.android.qjpmc.widget.retrofithelper.interceptor.HttpNativeStatusInterceptor;
import com.android.qjpmc.widget.retrofithelper.interceptor.NetworkInterceptor;
import com.android.qjpmc.widget.retrofithelper.interceptor.RetryInterceptor;
import com.android.qjpmc.widget.retrofithelper.interceptor.SecurityInterceptor;
import com.android.qjpmc.widget.retrofithelper.rxexception.TokenInvalidInterceptor;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class RetrofitAuthManager extends RetrofitManager {

    protected void init() {
        init(true);
    }

    protected void init(boolean isAddTokenInvalidInterceptor) {

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        /*ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                .tlsVersions(TlsVersion.TLS_1_2)
                .cipherSuites(
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
                        CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
                .build();
        httpClientBuilder.connectionSpecs(Collections.singletonList(spec));*/

        try {
      /*  SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, null, new SecureRandom());
        SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
            httpClientBuilder.sslSocketFactory(socketFactory);*/

            //  HttpsManager.setCertificates(httpClientBuilder, UIUtils.getContext().getAssets().open("perfee.crt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加头部
        httpClientBuilder.addInterceptor(new SecurityInterceptor());
        if (isAddTokenInvalidInterceptor)
            //token过期拦截
            httpClientBuilder.addInterceptor(new TokenInvalidInterceptor());
        //http状态码拦截
        httpClientBuilder.addInterceptor(new HttpNativeStatusInterceptor());
        //重试次数
        httpClientBuilder.addInterceptor(new RetryInterceptor(3));
        // 有网络的情况下,设置缓存
        // httpClientBuilder.addNetworkInterceptor(new CacheOnlineInterceptor());
        // 没有网络的清下,设置缓存
        httpClientBuilder.addInterceptor(new CacheOfflineInterceptor());
        // httpClientBuilder.addNetworkInterceptor(new StethoInterceptor());
        //声明缓存地址和大小
        File diskNetCacheFolderFile = FileUtils.getDiskNetCacheFolderFile();
        if (diskNetCacheFolderFile != null) {
            Cache cache = new Cache(diskNetCacheFolderFile, 50 * 1024 * 1024);
            httpClientBuilder.cache(cache);
        }
      //  if (!CommonUtils.isAppRelease()) {
            NetworkInterceptor networkInterceptor = new NetworkInterceptor();
            networkInterceptor.setLevel(NetworkInterceptor.Level.BODY);
            httpClientBuilder.addInterceptor(networkInterceptor);
      //  }

        httpClientBuilder.connectTimeout(24, TimeUnit.SECONDS);
        httpClientBuilder.readTimeout(24, TimeUnit.SECONDS);
        httpClientBuilder.writeTimeout(24, TimeUnit.SECONDS);
        httpClientBuilder.retryOnConnectionFailure(true);
        httpClientBuilder.followSslRedirects(true);
        httpClientBuilder.followRedirects(true);
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(Url.BASE_URL)
                .client(httpClientBuilder.build())
                .build();
        apiService = retrofit.create(ApiServer.class);
    }


}
